#include "CLogRecord.h"

void CLogRecord::AddField(const CLogFieldPtr& field)
{
	m_fields[field->GetName()] = field;
}

void CLogRecord::AddField(const std::string& fieldName, const CLogValue& value)
{
	m_fields[fieldName] = std::make_shared<CLogField>(fieldName, value);
}

std::map<std::string, CLogFieldPtr>& CLogRecord::GetFields()
{
	return m_fields;
}

CLogFieldPtr CLogRecord::getField(const std::string& fieldName)
{
	auto it = m_fields.find(fieldName);
	if (it != m_fields.end())
	{
		return it->second;
	}

	return nullptr;
}

bool CLogRecord::hasField(const std::string& fieldName) const
{
	return m_fields.find(fieldName) != m_fields.end();
}

int CLogRecord::enumFields(std::function<int(CLogFieldPtr& field)> func)
{
	for (auto& field : m_fields)
	{
		int ret = func(field.second);
		if (ret != 0)
		{
			return ret;
		}
	}

	return 0;
}

uint64_t CLogRecord::getRecordSize() const
{
	uint64_t nSize = 0;
	for (auto& field : m_fields)
	{
		nSize += field.second->getSize();
	}
	return nSize;
}